package com.ruoyi.project.cms.product.service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.project.cms.product.domain.Product;
import com.ruoyi.project.cms.product.domain.ProductPic;
import com.ruoyi.project.cms.product.mapper.PicMapper;
import com.ruoyi.project.cms.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @CreateTime 2020/5/21 21:18
 */
@Service
public class PicServiceImpl implements PicService {
    @Autowired
    private PicMapper picMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询所有图片
     */
    @Override
    public List<ProductPic> findAll() {
        return picMapper.selectAll();
    }

    /**
     * 根据ID查图片
     */
    @Override
    public Product findById(Long picId) {
        Product product = new Product();
        product.setProductPic(picMapper.selectById(picId));
        return product;
    }

    /**
     * 删除图片
     */
    @Override
    public int removePicById(Long picId) {
        ProductPic productPic = picMapper.selectById(picId);
        if (productMapper.selectIsUsing(picId,4)>0){
            throw new BusinessException(String.format("%1$s:该图片正在使用,不能删除", productPic.getPicName()));
        }
        return picMapper.deletePicById(picId);
    }
}
